package com.example.demo.clid;

import com.example.demo.dao1.ClassJpaDao;
import com.example.demo.entity.Classa;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class ClassaController {
    @Resource
    ClassJpaDao classJpaDao;

    public ClassaController() {
    }

    @GetMapping({"/findByName"})
    public List<Classa> findByName() {
        return this.classJpaDao.findByNameNotNull();
    }

    @GetMapping({"/findByName1"})
    public List<Classa> findByName1(int num) {
        return this.classJpaDao.findByNum(num);
    }

    @GetMapping({"/deleteJpa1"})
    public String deleteJpa(int id) {
        this.classJpaDao.deleteById(id);
        return "删除成功";
    }

    @GetMapping({"/insertJpa13"})
    public String insertJpa(int num, String name, String dep) {
        Classa classa = new Classa();
        classa.setNum(num);
        classa.setName(name);
        classa.setDep(dep);
        Classa result = (Classa)this.classJpaDao.save(classa);
        return result != null ? "插入成功" : "插入失败";
    }

    @GetMapping({"/updateJpa13"})
    public String updateJpa1(int id, String name) {
        Classa classa = (Classa)this.classJpaDao.findById(id).get();
        classa.setName(name);
        Classa result = (Classa)this.classJpaDao.save(classa);
        return result != null ? "更新成功" : "更新失败";
    }

    @GetMapping({"/findAllJpaByName133"})
    public List<Classa> findAllJpa1(String name, String dep) {
        return this.classJpaDao.findByNameContainingOrDep(name, dep);
    }
}
